
var userAuthorityMgrPanelOpt = {
    id:"userAuthorityMgrPanel",
    bombType:BOMB_TYPE2
};

//机构下拉树选择事件
function orgOnCheck(dom,id,treeNode) {

    var treeObj=$.fn.zTree.getZTreeObj("orgSel_tree"),
        nodes=treeObj.getCheckedNodes(true),
        v="";
    var userId = $("#userAuthorityMgr_userIId").val();
    for(var i=0;i<nodes.length;i++){
        if(i == nodes.length-1){
            v+=nodes[i].name
        }else{
            v+=nodes[i].name + ",";
        }
        // alert(nodes[i].id); //获取选中节点的值
    }
    // alert(v);
    if(treeNode.checked == true){
        var content = "<dl id='authorOrg_"+treeNode.realID+"'>" +
            "<dt><span>"+treeNode.name+"</span><input type='hidden' name='authorSelOrg' value='"+treeNode.realID+"'/><a class=\"authority_edit_btn\" href=\"javascript:void(0)\" onclick='editAuthorRole(this,\""+userId+"\",\""+treeNode.name+"\",\""+treeNode.realID+"\")'>编辑角色</a></dt>" +
            "<dd></dd>" +
            "</dl>";
        $("#selAuthority_content").append($(content));
        authorRightShow(0,treeNode.realID,treeNode.name);
    }else{
        $("#authorOrg_"+treeNode.realID).remove();
        if($("#selAuthority_content").find("dl").size() == 0 ){
            $("#user_systemAuthority_box").find(".selOrg_box").addClass("indexAuth_box")
            $("#user_systemAuthority_box").css("width","460px");
            $("#user_systemAuthority_box").find (".selRole_box").removeClass("indexAuth_box");
            $("#user_systemAuthority_box").find (".selRole_box").hide();

        }
    }

}




//提交编辑编辑用户角色
function toEditUserOrgRole(){
    var zTreeObj = $.fn.zTree.getZTreeObj("authorRole_tree");
    var checkedNodes = zTreeObj.getCheckedNodes();
    var orgId = $("#selAuthorty_SelOrg").val();
    var content = "";
    for(var i=0; i<checkedNodes.length;i++){
        content += "<span>"+checkedNodes[i].name+"<input type='hidden' name='authorSelRole' value='"+checkedNodes[i].id+"'/></span>"
    }
    $("#authorOrg_"+orgId).find("dd").html(content);
    $("#authorSelRole_box").hide();
    $("#authorityOrg_unsubmit").hide();
    $("#authorityOrg_submit").show();
}



//用户机构下拉框
function editAuthority(id,name) {
    $("#userAuthorityMgr_tit").find(".userAuthorityMgr_tab").eq(0).addClass("indexAuthTab");
    $("#userAuthorityMgr_tit").find(".userAuthorityMgr_tab").eq(1).removeClass("indexAuthTab");
    $("#checkAuthorityAllOrgBtn").attr("class","checkAllBtn uncheckBtn");
    $("#orgAllCheck").prop("checked",false);
    $("#user_systemAuthority_box").show();
    $("#user_businessAuthority_box").hide();
    $("#userAuthorityMgr_userIId").val(id);
    $.wdfProgress.progress("请稍等...",$("body"));
    var orgZTreeSetting = {
        data : {
            simpleData : {
                enable : true
            }
        },
        check: {
            enable: true,
            chkStyle:"checkbox",
            chkboxType: { "Y": "", "N": "" }
        },
        callback : {
            onCheck:orgOnCheck,
            onClick: function (e, treeId, treeNode, clickFlag) {
                var orgTree =  $.fn.zTree.getZTreeObj("orgSel_tree");
                orgTree.checkNode(treeNode, !treeNode.checked, true);

            }
        }
    };
    //

    $.ajax({
        url:basePath+"/department/listTree",
        data:{

        },
        dataType:"json",
        type: "post",
        contentType:"application/x-www-form-urlencoded",
        beforeSend:null,
        success:function(data){
            data = data.data;
            $.fn.zTree.init($("#orgSel_tree"),orgZTreeSetting,data);
            var orgTree =  $.fn.zTree.getZTreeObj("orgSel_tree");
            orgTree.expandAll(true);
            var userOrgArray=[];
            $.ajax({
                url:basePath+"/user/listUsersOrgsRoles",
                data:{
                    userIds:id
                },
                dataType:"json",
                type: "post",
                contentType:"application/x-www-form-urlencoded",
                beforeSend:null,
                success:function(data){

                    if(data.data.length>0){
                        var content="";
                        $(data.data).each(function () {
                            userOrgArray.push(this.orgId);
                            content += "<dl id='authorOrg_"+this.orgId+"'>" +
                                "<dt><span>"+this.orgName+"</span><input type='hidden' name='authorSelOrg' value='"+this.orgId+"'/><a class=\"authority_edit_btn\" href=\"javascript:void(0)\" onclick='editAuthorRole(this,\""+id+"\",\""+this.orgName+"\",\""+this.orgId+"\")'>编辑角色</a></dt>" +
                                "<dd>";
                            if(this.roleIds&&this.roleIds != "null"){
                                var roleIdsArr = this.roleIds.split(",");
                                var roleNameArr = this.roleNames.split(",");
                                for (var i=0;i<roleNameArr.length;i++){
                                    content +="<span>"+roleNameArr[i]+"<input type='hidden' name='authorSelRole' value='"+roleIdsArr[i]+"'/></span>";
                                }
                            }
                            content +=     "</dd>" +
                                "</dl>";
                            authorRightShow(0,this.orgId,this.orgName);
                        });
                        $("#selAuthority_content").html(content);
                        for (var i=0;i<userOrgArray.length;i++) {
                            var checkNode=orgTree.getNodeByParam("realID",userOrgArray[i], null);
                            orgTree.checkNode(checkNode,true,true);
                        }
                    }else{
                        //menusArray.push(rowData.menus[0].id);
                        $("#selAuthority_content").html("");
                        $("#user_systemAuthority_box").css("width","460px");
                        userAuthorityMgrPanel.find(".selRole_box").hide();
                        $("#userAuthorityMgr_tit").find(".userAuthorityMgr_tab").eq(0).addClass("indexAuthTab");
                        $("#userAuthorityMgr_tit").find(".userAuthorityMgr_tab").eq(1).removeClass("indexAuthTab");
                    }

                    userAuthorityMgrPanel.show();
                    $.wdfProgress.showMsg("数据加载成功！",$("body"),true);
                    // setTimeout(function(){
                        $.wdfProgress.progRemove($("body"));
                    // },100);
                },
                error:function (XMLHttpRequest, textStatus, errorThrown) {
                    //    $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
                }

            })

        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
        }
    });
    var roleZTreeSetting = {
        data : {
            simpleData : {
                enable : true
            }
        },
        check: {
            enable: true,
            chkStyle:"checkbox",
            chkboxType: { "Y": "", "N": "" }
        },
        callback : {
            onClick: function (e, treeId, treeNode, clickFlag) {
                var treeObj=$.fn.zTree.getZTreeObj("authorRole_tree");
                treeObj.checkNode(treeNode, !treeNode.checked, true);
            }
        }
    };
    $.ajax({
        url:basePath+"/role/listAllRoles",
        dataType:"json",
        type: "post",
        contentType:"application/x-www-form-urlencoded",
        beforeSend:null,
        success:function(data){
            data = data.data;
            $.fn.zTree.init($("#authorRole_tree"),roleZTreeSetting,data);




        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            //    $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
        }
    })



//项目标段面板
    var businessZTreeSetting = {
        data : {
            simpleData : {
                enable: true



            }
        },
        check: {
            enable: true,
            chkStyle:"checkbox",
            chkboxType: { "Y": "", "N": "" }
        },
        callback : {
            onCheck:businessOnCheck,
            onClick: function (e, treeId, treeNode, clickFlag) {
                var projectTree =  $.fn.zTree.getZTreeObj("projectSel_tree");
                projectTree.checkNode(treeNode, !treeNode.checked, true);
            }
        }
    };



    $.ajax({
        url:basePath+"/project/list",
        data:{

        },
        dataType:"json",
        type: "post",
        contentType:"application/x-www-form-urlencoded",
        beforeSend:null,
        success:function(data){
            data = data.data;
            for(var i=0;i<data.length;i++){
                data[i].id=data[i].projectId;
                data[i].name = data[i].projectName;
            }
            $.fn.zTree.init($("#projectSel_tree"),businessZTreeSetting,data);
            var projectTree =  $.fn.zTree.getZTreeObj("projectSel_tree");
            projectTree.expandAll(true);
            var userProjectArray=[];
            $.ajax({
                url:basePath+"/user/listUsersProjects",
                data:{
                    userIds:id
                },
                dataType:"json",
                type: "post",
                contentType:"application/x-www-form-urlencoded",
                beforeSend:null,
                success:function(data){

                    if(data.data.length>0){
                        var content="";
                        $(data.data).each(function () {
                            userProjectArray.push(this.id);
                            content += "<dl id='authorProject_"+this.id+"'>" +
                                "<dt><span>"+this.orgName+"</span><input type='hidden' name='authorSelProject' value='"+this.id+"'/><a class=\"authority_edit_btn\" href=\"javascript:void(0)\" onclick='editAuthorSection(this,\""+id+"\",\""+this.name+"\",\""+this.id+"\")'>编辑标段</a></dt>" +
                                "<dd>";
                            if(this.roleIds&&this.roleIds != "null"){
                                var roleIdsArr = this.roleIds.split(",");
                                var roleNameArr = this.roleNames.split(",");
                                for (var i=0;i<roleNameArr.length;i++){
                                    content +="<span>"+roleNameArr[i]+"<input type='hidden' name='authorSelRole' value='"+roleIdsArr[i]+"'/></span>";
                                }
                            }
                            content +=     "</dd>" +
                                "</dl>";
                        });
                        $("#section_content").html(content);

                    }else{
                        //menusArray.push(rowData.menus[0].id);
                    }
                    for (var i=0;i<userProjectArray.length;i++) {
                        var checkNode=projectTree.getNodeByParam("realID",userProjectArray[i], null);
                        projectTree.checkNode(checkNode,true,true);
                    }
                },
                error:function (XMLHttpRequest, textStatus, errorThrown) {
                    //    $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
                }

            })

        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
        }
    });


}


//显示权限面板右边
function authorRightShow(type,id,name){
    if(type == 0){//系统权限

        $("#user_systemAuthority_box").find(".selOrg_box").removeClass("indexAuth_box")
        $("#user_systemAuthority_box").css("width","1000px");
        $("#user_systemAuthority_box").find (".selRole_box").addClass("indexAuth_box");
        $("#user_systemAuthority_box").find (".selRole_box").show();

    }else if(type == 1){//业务权限
        $("#user_businessAuthority_box").find(".selProject_box").removeClass("indexAuth_box")
        $("#user_businessAuthority_box").css("width","1000px");
        $("#user_businessAuthority_box").find (".selBids_box").addClass("indexAuth_box");
        $("#user_businessAuthority_box").find (".selBids_box").show();
    }
}
//
function toSystem(){
            $("#checkAuthorityAllOrgBtn").attr("class","checkAllBtn uncheckBtn");
            $("#orgAllCheck").prop("checked",false);
            toSystemEditPanel();
            $("#userAuthorityMgr_tit").find(".userAuthorityMgr_tab").eq(0).addClass("indexAuthTab");
            $("#userAuthorityMgr_tit").find(".userAuthorityMgr_tab").eq(1).removeClass("indexAuthTab");
            $("#user_systemAuthority_box").show();
            $("#user_businessAuthority_box").hide();


}
function toBusiness(){
    $("#checkAuthorityAllProjectBtn").attr("class","checkAllBtn uncheckBtn");
    $("#projectAllCheck").prop("checked",false);

            toBusinessEditPanel();
            $("#userAuthorityMgr_tit").find(".userAuthorityMgr_tab").eq(1).addClass("indexAuthTab");
            $("#userAuthorityMgr_tit").find(".userAuthorityMgr_tab").eq(0).removeClass("indexAuthTab");
            $("#user_systemAuthority_box").hide();
            $("#user_businessAuthority_box").show();



}


function toSystemEditPanel() {
    var userId = $("#userAuthorityMgr_userIId").val();
    var orgTree =  $.fn.zTree.getZTreeObj("orgSel_tree");
    orgTree.expandAll(true);
    var userOrgArray=[];
    $.ajax({
        url:basePath+"/user/listUsersOrgsRoles",
        data:{
            userIds:userId
        },
        dataType:"json",
        type: "post",
        contentType:"application/x-www-form-urlencoded",
        beforeSend:null,
        success:function(data){

            if(data.data.length>0){
                var content="";
                $(data.data).each(function () {
                    userOrgArray.push(this.orgId);
                    content += "<dl id='authorOrg_"+this.orgId+"'>" +
                        "<dt><span>"+this.orgName+"</span><input type='hidden' name='authorSelOrg' value='"+this.orgId+"'/><a class=\"authority_edit_btn\" href=\"javascript:void(0)\" onclick='editAuthorRole(this,\""+userId+"\",\""+this.orgName+"\",\""+this.orgId+"\")'>编辑角色</a></dt>" +
                        "<dd>";
                    if(this.roleIds&&this.roleIds != "null"){
                        var roleIdsArr = this.roleIds.split(",");
                        var roleNameArr = this.roleNames.split(",");
                        for (var i=0;i<roleNameArr.length;i++){
                            content +="<span>"+roleNameArr[i]+"<input type='hidden' name='authorSelRole' value='"+roleIdsArr[i]+"'/></span>";
                        }
                    }
                    content +=     "</dd>" +
                        "</dl>";
                    authorRightShow(0,this.orgId,this.orgName);
                });
                $("#selAuthority_content").html(content);
                orgTree.checkAllNodes(false);
                for (var i=0;i<userOrgArray.length;i++) {
                    var checkNode=orgTree.getNodeByParam("realID",userOrgArray[i], null);

                    orgTree.checkNode(checkNode,true,true);
                }
            }else{
                //menusArray.push(rowData.menus[0].id);
                orgTree.checkAllNodes(false);
                $("#selAuthority_content").html("");
                $("#user_systemAuthority_box").css("width","460px");
                userAuthorityMgrPanel.find(".selRole_box").hide();
                $("#userAuthorityMgr_tit").find(".userAuthorityMgr_tab").eq(0).addClass("indexAuthTab");
                $("#userAuthorityMgr_tit").find(".userAuthorityMgr_tab").eq(1).removeClass("indexAuthTab");
            }

        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            //    $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
        }

    });

}


function editAuthorRole(e,userId,orgName,orgId){
    $("#authorRole_tit").html(orgName);
    $("#authorRole_tit").attr("title",orgName);
    $("#authorRole_tit").attr("title",orgName);
    $("#checkAuthorityAllRoleBtn").attr("class","checkAllBtn uncheckBtn");
    $("#roleAllCheck").prop("checked",false);
    $.ajax({
        url:basePath+"/user/listUsersOrgsRoles",
        data:{
            userIds:userId,
            orgIds:orgId
        },
        dataType:"json",
        type: "post",
        contentType:"application/x-www-form-urlencoded",
        beforeSend:null,
        success:function(data){
            data = data.data;
            var userRoles = new Array();
            if(data.length>0 && data[0].roleIds){
                userRoles = data[0].roleIds.split(",");
                var treeObj=$.fn.zTree.getZTreeObj("authorRole_tree");
                treeObj.checkAllNodes(false);
                for (var i=0;i<userRoles.length;i++) {
                    var checkNode=treeObj.getNodeByParam("id",userRoles[i], null);

                    treeObj.checkNode(checkNode,true,true);
                }
            }else{
                var treeObj=$.fn.zTree.getZTreeObj("authorRole_tree");
                treeObj.checkAllNodes(false);
            }
            $("#selAuthorty_SelOrg").val(orgId);
            $("#authorSelRole_box").show();
            $("#authorityOrg_unsubmit").show();
            $("#authorityOrg_submit").hide();

        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            //    $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
        }
    })
}

function toSubOrgRole(){
    var userId = $("#userAuthorityMgr_userIId").val();
    var orgLength = $("#selAuthority_content").find("dl").size();
    var strArr = new Array();
    for(var i=0;i<orgLength;i++ ){
        var orgId = $("#selAuthority_content").find("dl").eq(i).find("input[name=authorSelOrg]").val();
        if($("#selAuthority_content").find("dl").eq(i).find("dd").find("span").size()>0){
            $("#selAuthority_content").find("dl").eq(i).find("dd").find("span").each(function (j) {
                var obj = {
                    orgId:orgId,
                    roleId:$(this).find("input[name=authorSelRole]").val()
                };
                strArr.push(obj);
            })
        }else{
            var obj = {
                orgId:orgId
            };
            strArr.push(obj);
        }

    }
    var strJson = JSON.stringify(strArr);
    $.wdfProgress.progress("正在请求...", $("body"));
    $.ajax({
        url:basePath+"/user/changeUserOrgsRoles",
        data:{
            userId:userId,
            userOrgsRolesStr:strJson
        },
        dataType:"json",
        type: "post",
        contentType:"application/x-www-form-urlencoded",
        beforeSend:null,
        success:function(data){
            if(data.status){
                $.wdfProgress.showMsg(data.msg, $("body"));
                $.wdfProgress.progRemove($("body"), function() {
                }, 1666);
            }


        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
            $.wdfProgress.showMsg("系统错误或网络延迟！",$("body"),true);
        }
    })
}


function closeAuthority(){
    $.wdfProgress.progress("放弃当前编辑?",$("body"), function(){

    }, function(){

        $.wdfProgress.progRemove($("body"),function(){
            userAuthorityMgrPanel.find(".closeBtn").click();
            $("#userGrid").wdfGrid("reload",userGridOpt);
        },50);
    });

}


function orgAllOnCheck(treeNode) {

    var treeObj=$.fn.zTree.getZTreeObj("orgSel_tree"),
        nodes=treeObj.getCheckedNodes(true),
        v="";
    var userId = $("#userAuthorityMgr_userIId").val();
    for(var i=0;i<nodes.length;i++){
        if(i == nodes.length-1){
            v+=nodes[i].name
        }else{
            v+=nodes[i].name + ",";
        }
        // alert(nodes[i].id); //获取选中节点的值
    }
    // alert(v);
    if(treeNode.checked == true){
        var content = "<dl id='authorOrg_"+treeNode.realID+"'>" +
            "<dt><span>"+treeNode.name+"</span><input type='hidden' name='authorSelOrg' value='"+treeNode.realID+"'/><a class=\"authority_edit_btn\" href=\"javascript:void(0)\" onclick='editAuthorRole(this,\""+userId+"\",\""+treeNode.name+"\",\""+treeNode.realID+"\")'>编辑角色</a></dt>" +
            "<dd></dd>" +
            "</dl>";
        $("#selAuthority_content").append($(content));
        authorRightShow(0,treeNode.realID,treeNode.name);
    }else{
        $("#authorOrg_"+treeNode.realID).remove();
        if($("#selAuthority_content").find("dl").size() == 0 ){
            $("#user_systemAuthority_box").find(".selOrg_box").addClass("indexAuth_box")
            $("#user_systemAuthority_box").css("width","460px");
            $("#user_systemAuthority_box").find (".selRole_box").removeClass("indexAuth_box");
            $("#user_systemAuthority_box").find (".selRole_box").hide();

        }
    }

}


function checkAuthorityAllOrg(e){
    var orgTree =  $.fn.zTree.getZTreeObj("orgSel_tree");
    if($(e).hasClass("uncheckBtn")){
        $(e).removeClass("uncheckBtn");
        $(e).addClass("checkedBtn");
        $(e).prev("input[type=checkbox]").prop("checked",true);
        $("#selAuthority_content").html("");
        orgTree.checkAllNodes(true);
        var nodes=orgTree.getCheckedNodes(true);
        for (var i=0; i<nodes.length;i++){
            orgAllOnCheck(nodes[i]);
        }
    }else{
        $("#selAuthority_content").html("");
        $(e).removeClass("checkedBtn");
        $(e).addClass("uncheckBtn");
        $(e).prev("input[type=checkbox]").prop("checked",false);
        orgTree.checkAllNodes(false);
        $("#user_systemAuthority_box").find(".selOrg_box").addClass("indexAuth_box");
        $("#user_systemAuthority_box").css("width","460px");
        $("#user_systemAuthority_box").find (".selRole_box").removeClass("indexAuth_box");
        $("#user_systemAuthority_box").find (".selRole_box").hide();
    }
}

function checkAuthorityAllRole(e){
    var zTreeObj = $.fn.zTree.getZTreeObj("authorRole_tree");
    if($(e).hasClass("uncheckBtn")){
        $(e).removeClass("uncheckBtn");
        $(e).addClass("checkedBtn");
        $(e).prev("input[type=checkbox]").prop("checked",true);
        zTreeObj.checkAllNodes(true);

    }else{

        $(e).removeClass("checkedBtn");
        $(e).addClass("uncheckBtn");
        $(e).prev("input[type=checkbox]").prop("checked",false);
        zTreeObj.checkAllNodes(false);

    }
}


$(function () {
    userAuthorityMgrPanel=$("#userAuthorityMgrPanel").wdfPanel("FormPanel",userAuthorityMgrPanelOpt);
    $(document).click(function () {
        $("#authorSelRole_box").hide();
        $("#authorityOrg_unsubmit").hide();
        $("#authorityOrg_submit").show();
    })
    $("#authorSelRole_box").click(function (e) {
        stopPropagation(e);
    })
    $("#orgAllCheck").change(function () {
        var orgTree =  $.fn.zTree.getZTreeObj("orgSel_tree");
        if($(this).prop("checked") == true){
            $("#selAuthority_content").html("");
            orgTree.checkAllNodes(true);
            var nodes=orgTree.getCheckedNodes(true);
            for (var i=0; i<nodes.length;i++){
                orgAllOnCheck(nodes[i]);
            }
        }else{
            $("#selAuthority_content").html("");
            orgTree.checkAllNodes(false);
            $("#user_systemAuthority_box").find(".selOrg_box").addClass("indexAuth_box");
            $("#user_systemAuthority_box").css("width","460px");
            $("#user_systemAuthority_box").find (".selRole_box").removeClass("indexAuth_box");
            $("#user_systemAuthority_box").find (".selRole_box").hide();
        }
    })
    $("#roleAllCheck").change(function () {
        var zTreeObj = $.fn.zTree.getZTreeObj("authorRole_tree");
        if($(this).prop("checked") == true){

            zTreeObj.checkAllNodes(true);
        }else{
            zTreeObj.checkAllNodes(false);
        }

    })
});